﻿using System.Web.Mvc;
using Beeldbankmvc.Models;

namespace Beeldbankmvc.Controllers
{
    public class PublishRightsController : Controller
    {
        PublishRightsRepository publishRepository = new PublishRightsRepository();

        //
        // GET: /PublishRights/
        [Authorize(Roles = "Administrator")]
        public ActionResult Index()
        {
            // Get all publish rights as model
            var publishRightsModels = publishRepository.getPublishRights();

            return View(publishRightsModels);
        }

        //
        // GET: /PublishRights/Details/5
        [Authorize(Roles = "Administrator")]
        public ActionResult Details(int id)
        {
            var publishRightsModel = publishRepository.FindOne(id);
            publishRightsModel.beheerUserList = publishRepository.getBeheerUserList();

            return View(publishRightsModel);
        }

        //
        // GET: /PublishRights/Create
        [Authorize(Roles = "Administrator")]
        public ActionResult Create()
        {
            PublishRightsModel publishRightsModel = new PublishRightsModel();

            publishRightsModel.beheerUserList = publishRepository.getBeheerUserList();
            publishRightsModel.eindRedacteurUserList = publishRepository.getEindRedacteurUserList();

            return View(publishRightsModel);
        } 

        //
        // POST: /PublishRights/Create

        [HttpPost]
        [Authorize(Roles = "Administrator")]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                PublishRightsModel publishRightsModel = new PublishRightsModel();

                // these lists have to be updated for the update statement below to work
                publishRightsModel.beheerUserList = publishRepository.getBeheerUserList();
                publishRightsModel.eindRedacteurUserList = publishRepository.getEindRedacteurUserList();

                // update the model with all the form fields contained in the FormCollection
                UpdateModel(publishRightsModel);

                publishRepository.Create(publishRightsModel);
                publishRepository.Save();

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /PublishRights/Edit/5
        
        [Authorize(Roles = "Administrator")]
        public ActionResult Edit(int id)
        {
            var publishRightsModel = publishRepository.FindOne(id);

            publishRightsModel.beheerUserList = publishRepository.getBeheerUserList();
            publishRightsModel.eindRedacteurUserList = publishRepository.getEindRedacteurUserList();

            return View(publishRightsModel);
        }

        //
        // POST: /PublishRights/Edit/5

        [HttpPost]
        [Authorize(Roles = "Administrator")]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                var publishRightsModel = publishRepository.FindOne(id);

                // these lists have to be updated for the update statement below to work
                publishRightsModel.beheerUserList = publishRepository.getBeheerUserList();
                publishRightsModel.eindRedacteurUserList = publishRepository.getEindRedacteurUserList();

                // update the model with all the form fields contained in the FormCollection
                UpdateModel(publishRightsModel);

                publishRepository.Update(publishRightsModel);
                publishRepository.Save();
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /PublishRights/Delete/5

        [Authorize(Roles = "Administrator")]
        public ActionResult Delete(int id)
        {
            var publishRightsModel = publishRepository.FindOne(id);
            return View(publishRightsModel);
        }

        //
        // POST: /PublishRights/Delete/5

        [HttpPost]
        [Authorize(Roles = "Administrator")]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                publishRepository.Delete(id);
                publishRepository.Save();

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
